<?php

class Db_Joins_TablesJoins
{
	var $source_type;
	var $keyword;
	var $provinceId;
	var $cityId;
	var $authState_str;
	var $page_id;
	var $page_size = 30;
	
	var $status_str;
	var $skip_pagination;
	
	var $start_date_start;
	var $start_date_end;
	var $expired_date_start;
	var $expired_date_end;
	var $sales_emp_id;
	var $contract_type;
	var $renew_type;
	var $product_id;
	var $status;
	var $owner_id;
	
	var $date_start;
	var $date_end;
	var $task_id;
	var $error_msg;
	
	var $via;
	var $period_id;
	var $period_id_2;
	var $status_type;
	var $date_start_2;
	var $date_end_2;
	
	function GetDealers()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		if(1 == $this->source_type)
		{
			$source_table = "t_dealer_hxb";
		}else{
			$source_table = "t_dealer";
		}
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from($source_table, array("count(id) as ct"));
		
		if($this->provinceId)
		{
			$data->where("provinceId = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("cityId = ?", $this->cityId);
		}
		
		if($this->authState_str || is_numeric($this->authState_str))
		{
			$authState_arr = explode("_", $this->authState_str);
			$data->where("authState IN (?)", $authState_arr);
		}
		
		if($this->keyword)
		{
			$data->where("name like '%".$this->keyword."%' or companyName like '%".$this->keyword."%' or tel like '%".$this->keyword."%'");
		}

		$ct = $dealers_db->fetchRow($data);
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from($source_table, array("id as dealer_id", "name", "companyName", "provinceName", "cityName", "tel", "authState", "type"));
		
		if($this->provinceId)
		{
			$data->where("provinceId = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("cityId = ?", $this->cityId);
		}
		
		if($this->authState_str || is_numeric($this->authState_str))
		{
			$authState_arr = explode("_", $this->authState_str);
			$data->where("authState IN (?)", $authState_arr);
		}
		
		if($this->keyword)
		{
			$data->where("name like '%".$this->keyword."%' or companyName like '%".$this->keyword."%' or tel like '%".$this->keyword."%'");
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"name" => array(),
				"companyName" => array(),
				"provinceName" => array(),
				"cityName" => array(),
				"tel" => array(),
				"authState" => array(),
				"type" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['name'][$row['dealer_id']] = $row['name'];
				$current_page_info['companyName'][$row['dealer_id']] = $row['companyName'];
				$current_page_info['provinceName'][$row['dealer_id']] = $row['provinceName'];
				$current_page_info['cityName'][$row['dealer_id']] = $row['cityName'];
				$current_page_info['tel'][$row['dealer_id']] = $row['tel'];
				$current_page_info['authState'][$row['dealer_id']] = $row['authState'];
				$current_page_info['type'][$row['dealer_id']] = $row['type'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetSigners()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
        
        $mod_params = new Db_Tables2_BDictParams();
        $sys_id = $mod_params->GetSysId();
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from("dealers_db".$sys_id.".b_signer as s", array("count(signer_id) as ct"));
		
		if($this->provinceId)
		{
			$data->where("s.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("s.city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status = explode("_", $this->status_str);
			$data->where("s.status IN (?)", $status);
		}
		
		if($this->keyword)
		{
			$data->where("s.company_name like '%".$this->keyword."%' or s.register_address like '%".$this->keyword."%' or s.contact_address like '%".$this->keyword."%' or s.zipcode like '%".$this->keyword."%' or s.tel like '%".$this->keyword."%' or s.fax like '%".$this->keyword."%' or s.company_owner like '%".$this->keyword."%' or s.business_number like '%".$this->keyword."%' or s.tax_number like '%".$this->keyword."%' or s.bank_number like '%".$this->keyword."%'");
		}
		
		$ct = $dealers_db->fetchRow($data);
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("dealers_db".$sys_id.".b_signer as s", array(
			"signer_id",
			"company_name",
			"province_name",
			"city_name",
			"register_address",
			"contact_address",
			"zipcode",
			"tel",
			"fax",
			"company_owner",
			"business_number",
			"tax_number",
			"bank_name",
			"bank_number",
			"business_expired_date",
			"is_regular_taxpayer",
			"img1",
			"img2",
			"img3",
			"img4",
			"status",
			"emp_id",
			"ctime"));
		$data->joinLeft("hxb_db.auth_emp as e", "e.id=s.emp_id", array("realName"));
		
		if($this->provinceId)
		{
			$data->where("s.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("s.city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status = explode("_", $this->status_str);
			$data->where("s.status IN (?)", $status);
		}
		
		if($this->keyword)
		{
			$data->where("s.company_name like '%".$this->keyword."%' or s.register_address like '%".$this->keyword."%' or s.contact_address like '%".$this->keyword."%' or s.zipcode like '%".$this->keyword."%' or s.tel like '%".$this->keyword."%' or s.fax like '%".$this->keyword."%' or s.company_owner like '%".$this->keyword."%' or s.business_number like '%".$this->keyword."%' or s.tax_number like '%".$this->keyword."%' or s.bank_number like '%".$this->keyword."%'");
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("s.signer_id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"company_name" => array(),
				"province_name" => array(),
				"city_name" => array(),
				"register_address" => array(),
				"contact_address" => array(),
				"zipcode" => array(),
				"tel" => array(),
				"fax" => array(),
				"company_owner" => array(),
				"business_number" => array(),
				"tax_number" => array(),
				"bank_name" => array(),
				"bank_number" => array(),
				"business_expired_date" => array(),
				"is_regular_taxpayer" => array(),
				"img1" => array(),
				"img2" => array(),
				"img3" => array(),
				"img4" => array(),
				"status" => array(),
				"ctime" => array(),
				"emp_id" => array(),
				"emp_name" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['company_name'][$row['signer_id']] = $row['company_name'];
				$current_page_info['province_name'][$row['signer_id']] = $row['province_name'];
				$current_page_info['city_name'][$row['signer_id']] = $row['city_name'];
				$current_page_info['register_address'][$row['signer_id']] = $row['register_address'];
				$current_page_info['contact_address'][$row['signer_id']] = $row['contact_address'];
				$current_page_info['zipcode'][$row['signer_id']] = $row['zipcode'];
				$current_page_info['tel'][$row['signer_id']] = $row['tel'];
				$current_page_info['fax'][$row['signer_id']] = $row['fax'];
				$current_page_info['company_owner'][$row['signer_id']] = $row['company_owner'];
				$current_page_info['business_number'][$row['signer_id']] = $row['business_number'];
				$current_page_info['tax_number'][$row['signer_id']] = $row['tax_number'];
				$current_page_info['bank_name'][$row['signer_id']] = $row['bank_name'];
				$current_page_info['bank_number'][$row['signer_id']] = $row['bank_number'];
				$current_page_info['business_expired_date'][$row['signer_id']] = $row['business_expired_date'];
				$current_page_info['is_regular_taxpayer'][$row['signer_id']] = $row['is_regular_taxpayer'];
				$current_page_info['img1'][$row['signer_id']] = $row['img1'];
				$current_page_info['img2'][$row['signer_id']] = $row['img2'];
				$current_page_info['img3'][$row['signer_id']] = $row['img3'];
				$current_page_info['img4'][$row['signer_id']] = $row['img4'];
				$current_page_info['status'][$row['signer_id']] = $row['status'];
				$current_page_info['ctime'][$row['signer_id']] = $row['ctime'];
				$current_page_info['emp_id'][$row['signer_id']] = $row['emp_id'];
				$current_page_info['emp_name'][$row['signer_id']] = $row['realName'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetOwners()
	{
		$result = array(
			'current_page_info' => array()
		);
		
		$dealers_db = Zend_Registry::get('dealers_db');
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("b_owner", "*");
		
		$data->order(array("owner_id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"owner_name" => array(),
				"status" => array(),
				"ctime" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['owner_name'][$row['owner_id']] = $row['owner_name'];
				$current_page_info['status'][$row['owner_id']] = $row['status'];
				$current_page_info['ctime'][$row['owner_id']] = $row['ctime'];
			}
		}
		
		$result = array(
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetBus()
	{
		$result = array(
			'current_page_info' => array()
		);
		
		$dealers_db = Zend_Registry::get('dealers_db');
        
        $mod_params = new Db_Tables2_BDictParams();
        $sys_id = $mod_params->GetSysId();
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("dealers_db".$sys_id.".b_bu as b", array("bu_id", "bu_name", "emp_id", "ctime", "status"));
		$data->joinLeft("hxb_db".$sys_id.".auth_emp as e", "e.id=b.emp_id", array("realName"));
		
		$data->order(array("b.bu_id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"bu_name" => array(),
				"emp_name" => array(),
				"emp_id" => array(),
				"status" => array(),
				"ctime" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['bu_name'][$row['bu_id']] = $row['bu_name'];
				$current_page_info['emp_name'][$row['bu_id']] = $row['realName'];
				$current_page_info['emp_id'][$row['bu_id']] = $row['emp_id'];
				$current_page_info['status'][$row['bu_id']] = $row['status'];
				$current_page_info['ctime'][$row['bu_id']] = $row['ctime'];
			}
		}
		
		$result = array(
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetContracts()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from("b_contract_sku as s", array("count(cs_id) as ct"));
		$data->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array());
		$data->joinLeft("b_client as l", "l.client_id=c.client_id", array());
		
		if($this->contract_type_id)
		{
			$data->where("c.contract_type_id = ?", $this->contract_type_id);
		}
		
		if($this->pay_type)
		{
			$data->where("c.pay_type = ?", $this->pay_type);
		}
		
		if($this->start_date_start)
		{
			$data->where("c.start_date >= ?", $this->start_date_start);
		}
		
		if($this->start_date_end)
		{
			$data->where("c.start_date <= ?", $this->start_date_end);
		}
		
		if($this->expired_date_start)
		{
			$data->where("c.expired_date >= ?", $this->expired_date_start);
		}
		
		if($this->expired_date_end)
		{
			$data->where("c.expired_date <= ?", $this->expired_date_end);
		}
		
		if($this->sales_emp_id)
		{
			$data->where("c.sales_emp_id = ".$this->sales_emp_id." or l.emp_id=".$this->sales_emp_id);
		}elseif("LEVEL-2" == $_SESSION['identity']['level'] && 626 != $_SESSION['identity']['userid']) //销售主管只可以看属于自己的员工合同
		{
			$mod_user = new Db_Tables2_User();
			$my_staff_ids = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			
			if(!empty($my_staff_ids))
			{
				$data->where("c.sales_emp_id IN (?)", $my_staff_ids);
			}
		}
		
		if($this->product_id)
		{
			$data->where("s.product_id = ?", $this->product_id);
		}
		
		if($this->owner_id)
		{
			$data->where("c.owner_id = ?", $this->owner_id);
		}
		
		if($this->contract_type)
		{
			if(1 == $this->contract_type)
			{
				$data->where("c.parent_contract_id is NULL or parent_contract_id=''");
			}elseif(2 == $this->contract_type)
			{
				$data->where("c.parent_contract_id is not NULL and parent_contract_id!=''");
			}
		}
		
		if($this->renew_type)
		{
			$date_30 = date("Y-m-d", mktime(1,1,1, date("m"), date("d")+30, date("Y")));
			
			if(1 == $this->renew_type)
			{
				$data->where("c.expired_date <= ?", $date_30);
			}elseif(2 == $this->renew_type)
			{
				$data->where("c.expired_date <= ?", date("Y-m-d"));
			}elseif(3 == $this->renew_type)
			{
				$data->where("c.expired_date > ?", $date_30);
			}
		}
		
		if($this->provinceId)
		{
			$data->where("c.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("c.city_id = ?", $this->cityId);
		}
		
		if(is_numeric($this->status))
		{
			$data->where("c.status = ?", $this->status);
		}else{
			$data->where("c.status != ?", 7); //默认不显示作废
		}
		
		if($this->keyword)
		{
			$data->where("c.contract_sid like '%".$this->keyword."%' or c.parent_contract_sid like '%".$this->keyword."%' or c.client_name like '%".$this->keyword."%' or c.signer_company_name like '%".$this->keyword."%'");
		}
		
		if($this->via)
		{
			$emp_id_arr = array();
			
			if("LEVEL-1" == $_SESSION['identity']['level'])
			{
				$emp_id_arr[] = $_SESSION['identity']['userid'];
			}elseif("LEVEL-2" == $_SESSION['identity']['level'])
			{
				$mod_user = new Db_Tables2_User();
				$emp_id_arr = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			}
			
			if('index' == $this->via)
			{
				$data->where("c.allow_edit = ?", 0);
			}elseif('index1' == $this->via)
			{
				$data->where("c.status IN (?)", array(0,1,2));
			}elseif('index2' == $this->via)
			{
				$target_contract_id_arr = array();
				
				$select = $dealers_db->select();
				$select->from("b_contract_sku as s", array("sku_id"));
				$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
				$select->where("c.expired_date >= ?", $this->date_start);
				$select->where("c.expired_date <= ?", $this->date_end);
				$select->where("c.launch_switcher = ?", 2);
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				$client_sku = array();
				$current_client_sku = array();
				$client_ids = array();

				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$client_ids[] = $row['client_id'];

						$client_sku_str = $row['client_id']."-".$row['sku_id'];

						if(!in_array($client_sku_str, $client_sku))
						{
							$client_sku[] = $client_sku_str;
						}
					}
				}

				if(!empty($client_sku))
				{
					$client_ids = array_unique($client_ids);

					if(!empty($client_ids))
					{
						$select = $dealers_db->select();
						$select->from("b_contract_sku as s", array("sku_id"));
						$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
						$select->where("c.start_date >= ?", $this->date_start);
						$select->where("c.start_date <= ?", $this->date_end);
						$select->where("c.status != ?", 7);
						$select->where("c.client_id IN (?)", $client_ids);
						$select->where("c.contract_sid is not null or contract_sid != ''");
						$rows = $dealers_db->fetchAll($select);
						
						if(!empty($rows))
						{
							foreach($rows as $row)
							{
								$current_client_sku_str = $row['client_id']."-".$row['sku_id'];

								if(!in_array($current_client_sku_str, $current_client_sku))
								{
									$current_client_sku[] = $current_client_sku_str;
								}
							}
						}
					}
				}

				$lost_clients_arr = array_diff($client_sku, $current_client_sku);
				
				if(!empty($lost_clients_arr))
				{
					foreach($lost_clients_arr as $lstr)
					{
						$exp = explode("-", $lstr);
						
						$select = $dealers_db->select();
						$select->from("b_contract_sku as s", array());
						$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("contract_id"));
						$select->where("c.expired_date >= ?", $this->date_start);
						$select->where("c.expired_date <= ?", $this->date_end);
						$select->where("c.launch_switcher = ?", 2);
						$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
						$select->where("c.contract_sid is not null or contract_sid != ''");
						$select->where("c.client_id = ?", $exp[0]);
						$select->where("s.sku_id = ?", $exp[1]);
						$rows = $dealers_db->fetchAll($select);
						
						if(!empty($rows))
						{
							foreach($rows as $row)
							{
								$target_contract_id_arr[] = $row['contract_id'];
							}
						}
					}
				}
				
				if(!empty($target_contract_id_arr))
				{
					$data->where("c.contract_id IN (?)", $target_contract_id_arr);
				}else{
					$data->where("c.contract_id < ?", 0); //不显示任何数
				}
			}elseif('index3' == $this->via)
			{
				$target_contract_id_arr = array();
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id", "receivable_value", "received_value"));
				$select->where("c.status != ?", 7);
				$select->where("c.launch_switcher = ?", 2);
				$select->where("c.start_date <= ?", $this->date_end);
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$select->where("receivable_value > received_value");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$target_contract_id_arr[] = $row['contract_id'];
					}
				}
				
				if(!empty($target_contract_id_arr))
				{
					$data->where("c.contract_id IN (?)", $target_contract_id_arr);
				}else{
					$data->where("c.contract_id < ?", 0); //不显示任何数
				}
			}
		}
		
		$ct = $dealers_db->fetchRow($data);
		
//		var_dump($ct);die;
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("b_contract_sku as k", array("product_id", "sku_id"));
		$data->joinLeft("b_contract as c", "c.contract_id=k.contract_id", array(
			"contract_id",
			"contract_sid",
			"parent_contract_id",
			"parent_contract_sid",
			"city_name",
			"start_date",
			"expired_date",
			"sign_date",
			"sales_emp_id",
			"sales_emp_name",
			"client_name",
			"signer_company_name",
			"owner_name",
			"contract_value",
			"receivable_value",
			"received_value",
			"invoice_done",
			"tips",
			"status",
			"launch_switcher",
			"allow_edit",
			"ctime"));
		$data->joinLeft("b_client as l", "l.client_id=c.client_id", array("province_name", "city_name"));
		$data->joinLeft("b_contract_type as p", "p.contract_type_id=c.contract_type_id", array("contract_type_name"));
		$data->joinLeft("b_dict_delivery_status as s", "s.ds_id=c.delivery_status", array("name as delivery_status_name"));
		
		if($this->contract_type_id)
		{
			$data->where("c.contract_type_id = ?", $this->contract_type_id);
		}
		
		if($this->pay_type)
		{
			$data->where("c.pay_type = ?", $this->pay_type);
		}
		
		if($this->start_date_start)
		{
			$data->where("c.start_date >= ?", $this->start_date_start);
		}
		
		if($this->start_date_end)
		{
			$data->where("c.start_date <= ?", $this->start_date_end);
		}
		
		if($this->expired_date_start)
		{
			$data->where("c.expired_date >= ?", $this->expired_date_start);
		}
		
		if($this->expired_date_end)
		{
			$data->where("c.expired_date <= ?", $this->expired_date_end);
		}
		
		if($this->product_id)
		{
			$data->where("k.product_id = ?", $this->product_id);
		}
		
		if($this->sales_emp_id)
		{
			$data->where("c.sales_emp_id = ".$this->sales_emp_id." or l.emp_id=".$this->sales_emp_id);
		}elseif("LEVEL-2" == $_SESSION['identity']['level'] && 626 != $_SESSION['identity']['userid']) //销售主管只可以看属于自己的员工合同
		{
			$mod_user = new Db_Tables2_User();
			$my_staff_ids = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			
			if(!empty($my_staff_ids))
			{
				$data->where("c.sales_emp_id IN (?)", $my_staff_ids);
			}
		}
		
		if($this->owner_id)
		{
			$data->where("c.owner_id = ?", $this->owner_id);
		}
		
		if($this->contract_type)
		{
			if(1 == $this->contract_type)
			{
				$data->where("c.parent_contract_id is NULL or c.parent_contract_id=''");
			}elseif(2 == $this->contract_type)
			{
				$data->where("c.parent_contract_id is not NULL and c.parent_contract_id!=''");
			}
		}
		
		if($this->renew_type)
		{
			$date_30 = date("Y-m-d", mktime(1,1,1, date("m"), date("d")+30, date("Y")));
			
			if(1 == $this->renew_type)
			{
				$data->where("c.expired_date <= ?", $date_30);
			}elseif(2 == $this->renew_type)
			{
				$data->where("c.expired_date <= ?", date("Y-m-d"));
			}elseif(3 == $this->renew_type)
			{
				$data->where("c.expired_date > ?", $date_30);
			}
		}
		
		if($this->provinceId)
		{
			$data->where("l.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("l.city_id = ?", $this->cityId);
		}
		
		if(is_numeric($this->status))
		{
			$data->where("c.status = ?", $this->status);
		}else{
			$data->where("c.status != ?", 7); //默认不显示作废
		}
		
		if($this->keyword)
		{
			$data->where("c.contract_sid like '%".$this->keyword."%' or c.parent_contract_sid like '%".$this->keyword."%' or c.client_name like '%".$this->keyword."%' or c.signer_company_name like '%".$this->keyword."%'");
		}
		
		if($this->via)
		{
			if('index' == $this->via)
			{
				$data->where("c.allow_edit = ?", 0);
			}elseif('index1' == $this->via)
			{
				$data->where("c.status IN (?)", array(0,1,2));
			}elseif('index2' == $this->via)
			{
				$target_contract_id_arr = array();
				
				$select = $dealers_db->select();
				$select->from("b_contract_sku as s", array("sku_id"));
				$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
				$select->where("c.expired_date >= ?", $this->date_start);
				$select->where("c.expired_date <= ?", $this->date_end);
				$select->where("c.launch_switcher = ?", 2);
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				$client_sku = array();
				$current_client_sku = array();
				$client_ids = array();

				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$client_ids[] = $row['client_id'];

						$client_sku_str = $row['client_id']."-".$row['sku_id'];

						if(!in_array($client_sku_str, $client_sku))
						{
							$client_sku[] = $client_sku_str;
						}
					}
				}

				if(!empty($client_sku))
				{
					$client_ids = array_unique($client_ids);

					if(!empty($client_ids))
					{
						$select = $dealers_db->select();
						$select->from("b_contract_sku as s", array("sku_id"));
						$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
						$select->where("c.start_date >= ?", $this->date_start);
						$select->where("c.start_date <= ?", $this->date_end);
						$select->where("c.status != ?", 7);
						$select->where("c.client_id IN (?)", $client_ids);
						$select->where("c.contract_sid is not null or contract_sid != ''");
						$rows = $dealers_db->fetchAll($select);
						
						if(!empty($rows))
						{
							foreach($rows as $row)
							{
								$current_client_sku_str = $row['client_id']."-".$row['sku_id'];

								if(!in_array($current_client_sku_str, $current_client_sku))
								{
									$current_client_sku[] = $current_client_sku_str;
								}
							}
						}
					}
				}

				$lost_clients_arr = array_diff($client_sku, $current_client_sku);
				
				if(!empty($lost_clients_arr))
				{
					foreach($lost_clients_arr as $lstr)
					{
						$exp = explode("-", $lstr);
						
						$select = $dealers_db->select();
						$select->from("b_contract_sku as s", array());
						$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("contract_id"));
						$select->where("c.expired_date >= ?", $this->date_start);
						$select->where("c.expired_date <= ?", $this->date_end);
						$select->where("c.launch_switcher = ?", 2);
						$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
						$select->where("c.contract_sid is not null or contract_sid != ''");
						$select->where("c.client_id = ?", $exp[0]);
						$select->where("s.sku_id = ?", $exp[1]);
						$rows = $dealers_db->fetchAll($select);
						
						if(!empty($rows))
						{
							foreach($rows as $row)
							{
								$target_contract_id_arr[] = $row['contract_id'];
							}
						}
					}
				}
				
				if(!empty($target_contract_id_arr))
				{
					$data->where("c.contract_id IN (?)", $target_contract_id_arr);
				}else{
					$data->where("c.contract_id < ?", 0); //不显示任何数
				}
			}elseif('index3' == $this->via)
			{
				$target_contract_id_arr = array();
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id", "receivable_value", "received_value"));
				$select->where("c.status != ?", 7);
				$select->where("c.launch_switcher = ?", 2);
				$select->where("c.start_date <= ?", $this->date_end);
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$select->where("receivable_value > received_value");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					foreach($rows as $row)
					{
						$target_contract_id_arr[] = $row['contract_id'];
					}
				}
				
				if(!empty($target_contract_id_arr))
				{
					$data->where("c.contract_id IN (?)", $target_contract_id_arr);
				}else{
					$data->where("c.contract_id < ?", 0); //不显示任何数
				}
			}
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("c.contract_id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{	
			$ids = array();
			
			$current_page_info = array(
				"contract_sid" => array(),
				"province_name" => array(),
				"city_name" => array(),
				"start_date" => array(),
				"expired_date" => array(),
				"sign_date" => array(),
				"sales_emp_id" => array(),
				"sales_emp_name" => array(),
				"client_name" => array(),
				"signer_company_name" => array(),
				"owner_name" => array(),
				"contract_value" => array(),
				"receivable_value" => array(),
				"received_value" => array(),
				"status" => array(),
				"launch_switcher" => array(),
				"ctime" => array(),
				"channel_info" => array(),
				"service_info" => array(),
				"contract_type_name" => array(),
				"delivery_status_name" => array(),
				"next_leader_id" => array(),
				"next_leader_name" => array(),
				"tips" => array(),
				"allow_edit" => array(),
				"reject_reason" => array(),
				"reject_emp" => array(),
				"product_id" => array(),
				"sku_id" => array(),
				"invoice_done" => array()
			);
			
			$mod_approval = new Db_Tables2_BLogsSalesApproval();
			$mod_rejection = new Db_Tables2_BContractRejection();
			
			foreach($origine as $row)
			{
				$ids[] = $row['contract_id'];
				
				$current_page_info['contract_sid'][$row['contract_id']] = $row['contract_sid'];
				$current_page_info['province_name'][$row['contract_id']] = $row['province_name'];
				$current_page_info['city_name'][$row['contract_id']] = $row['city_name'];
				$current_page_info['start_date'][$row['contract_id']] = $row['start_date'];
				$current_page_info['expired_date'][$row['contract_id']] = $row['expired_date'];
				$current_page_info['sign_date'][$row['contract_id']] = $row['sign_date'];
				$current_page_info['sales_emp_id'][$row['contract_id']] = $row['sales_emp_id'];
				$current_page_info['sales_emp_name'][$row['contract_id']] = $row['sales_emp_name'];
				$current_page_info['client_name'][$row['contract_id']] = $row['client_name'];
				$current_page_info['signer_company_name'][$row['contract_id']] = $row['signer_company_name'];
				$current_page_info['owner_name'][$row['contract_id']] = $row['owner_name'];
				$current_page_info['contract_value'][$row['contract_id']] = $row['contract_value'];
				$current_page_info['receivable_value'][$row['contract_id']] = $row['receivable_value'];
				$current_page_info['received_value'][$row['contract_id']] = $row['received_value'];
				$current_page_info['status'][$row['contract_id']] = $row['status'];
				$current_page_info['launch_switcher'][$row['contract_id']] = $row['launch_switcher'];
				$current_page_info['ctime'][$row['contract_id']] = $row['ctime'];
				$current_page_info['contract_type_name'][$row['contract_id']] = $row['contract_type_name'];
				$current_page_info['delivery_status_name'][$row['contract_id']] = $row['delivery_status_name'];
				$current_page_info['tips'][$row['contract_id']] = $row['tips'];
				$current_page_info['allow_edit'][$row['contract_id']] = $row['allow_edit'];
				$current_page_info['product_id'][$row['contract_id']] = $row['product_id'];
				$current_page_info['sku_id'][$row['contract_id']] = $row['sku_id'];
				$current_page_info['invoice_done'][$row['contract_id']] = $row['invoice_done'];
				
				if(0 == $row['status'])
				{
					if($row['allow_edit'])
					{
						$mod_rejection->contract_id = $row['contract_id'];
						$reject = $mod_rejection->GetRecord();
						
						if($reject['result'])
						{
							$current_page_info['reject_reason'][$row['contract_id']] = $reject['reason'];
							$current_page_info['reject_emp'][$row['contract_id']] = $reject['emp_name'];
						}
					}else{
						$next = $mod_approval->GetNext(2, $row['contract_id']);

						if(!empty($next))
						{
							$current_page_info['next_leader_id'][$row['contract_id']] = $next['emp_id'];
							$current_page_info['next_leader_name'][$row['contract_id']] = $next['emp_name'];
						}
					}
				}
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
//		$mod_ext = new Sys_Extensions();
//		$mod_ext->FormatArray($result);
//		die;
		return $result;
	}
	
	function GetIndexInfo()
	{
		//load class
		$mod_sys = new Sys_Extensions();
		$dealers_db = Zend_Registry::get('dealers_db');
		
		//initial
		if($this->date_start)
		{
			$date_start = $this->date_start;
			$time_start = $this->date_start." 00:00:00";
		}else{
			$date_start = date("Y-m-d", mktime(0,0,0,date("m"), 1, date("Y")));
			$time_start = date("Y-m-d H:i:s", mktime(0,0,0,date("m"), 1, date("Y")));
		}
		
		if($this->date_end)
		{
			$date_end = $this->date_end;
			$time_end = $this->date_end." 23:59:59";
		}else{
			$date_end = date("Y-m-d");
			$time_end = date("Y-m-d H:i:s");
		}
		
		if($this->provinceId)
		{
			$provinceId = $this->provinceId;
		}else{
			$provinceId = NULL;
		}
		
		if($this->cityId)
		{
			$cityId = $this->cityId;
		}else{
			$cityId = NULL;
		}
		
		//销售
		if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2", "LEVEL-3", "LEVEL-4")))
		{
			$emp_id_arr = array();
			
			if("LEVEL-1" == $_SESSION['identity']['level'])
			{
				$emp_id_arr[] = $_SESSION['identity']['userid'];
			}elseif("LEVEL-2" == $_SESSION['identity']['level'])
			{
				$mod_user = new Db_Tables2_User();
				$emp_id_arr = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			}
			
			$result = array(
				"pending_approval_contracts" => 0,
				"lost_clients" => 0,
				"qiankuan_total" =>0,
				"products" => array()
			);
			
			if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
			{
				$select = $dealers_db->select();
				$select->from("b_contract", array("count(*) as ct"));

				if("LEVEL-1" == $_SESSION['identity']['level'])
				{
					$select->where("status IN (?)", array(0,1,2));
				}else{
					$select->where("status = ?", 0);
					$select->where("allow_edit = ?", 0);
				}

				$select->where("sales_emp_id IN (?)", $emp_id_arr);

				$row = $dealers_db->fetchRow($select);

				$result['pending_approval_contracts'] = $row['ct'];
			}elseif("LEVEL-3" == $_SESSION['identity']['level'])
			{
				$select = $dealers_db->select();
				$select->from("b_contract", array("count(*) as ct"));
				$select->where("status = ?", 1);
				$row = $dealers_db->fetchRow($select);

				$result['pending_approval_contracts'] = $row['ct'];
			}elseif("LEVEL-4" == $_SESSION['identity']['level'])
			{
				$select = $dealers_db->select();
				$select->from("b_contract", array("count(*) as ct"));
				$select->where("status = ?", 2);
				$row = $dealers_db->fetchRow($select);

				$result['pending_approval_contracts'] = $row['ct'];
			}
			
			
			$select = $dealers_db->select();
			$select->from("b_contract_sku as s", array("sku_id"));
			$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
			$select->where("c.expired_date >= ?", $date_start);
			$select->where("c.expired_date <= ?", $date_end);
			$select->where("c.launch_switcher = ?", 2);
			
			if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
			{
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
			}
			
			$select->where("c.contract_sid is not null or contract_sid != ''");
			$rows = $dealers_db->fetchAll($select);
			
			$client_sku = array();
			$current_client_sku = array();
			$client_ids = array();
			
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					$client_ids[] = $row['client_id'];
					
					$client_sku_str = $row['client_id']."-".$row['sku_id'];
					
					if(!in_array($client_sku_str, $client_sku))
					{
						$client_sku[] = $client_sku_str;
					}
				}
			}
			
			if(!empty($client_sku))
			{
				$client_ids = array_unique($client_ids);
				
				if(!empty($client_ids))
				{
					$select = $dealers_db->select();
					$select->from("b_contract_sku as s", array("sku_id"));
					$select->joinLeft("b_contract as c", "c.contract_id=s.contract_id", array("client_id"));
					$select->where("c.start_date >= ?", $date_start);
					$select->where("c.start_date <= ?", $date_end);
					$select->where("c.status != ?", 7);
					$select->where("c.client_id IN (?)", $client_ids);
					$select->where("c.contract_sid is not null or contract_sid != ''");
					$rows = $dealers_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$current_client_sku_str = $row['client_id']."-".$row['sku_id'];
							
							if(!in_array($current_client_sku_str, $current_client_sku))
							{
								$current_client_sku[] = $current_client_sku_str;
							}
						}
					}
				}
			}
			
			$lost_clients_arr = array_diff($client_sku, $current_client_sku);
			
			$result['lost_clients'] = count($lost_clients_arr);
			
			$select = $dealers_db->select();
			$select->from("b_contract as c", array("sum(receivable_value) as vl", "sum(received_value) as received"));
			$select->where("c.status != ?", 7);
			$select->where("c.launch_switcher = ?", 2);
			$select->where("c.start_date <= ?", $date_end);
			
			if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
			{
				$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
			}
			
			$select->where("c.contract_sid is not null or contract_sid != ''");
			$select->where("c.receivable_value > c.received_value");
			$row = $dealers_db->fetchRow($select);
			
			$result['qiankuan_total'] = $row['vl'] - $row['received'];
			
			//分产品
			$mod_products = new Db_Tables2_BDictProduct();
			$products = $mod_products->GetProducts();
			
			foreach($products as $product_id => $product_name)
			{
				$product_info = array(
					"product_name" => $product_name,
					"qiandan" => 0, //签单额
					"huikuan" => 0, //回款额
					"qiankuan" => 0, //欠款额
					"daoqi" => 0, //到期合同数
					"zhixing" => 0, //在执行合同数
					"xinqian" => 0 //新签合同数
				);
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id", "receivable_value", "received_value"));
				$select->joinLeft("b_contract_sku as s", "c.contract_id=s.contract_id", array());
				$select->where("c.sign_date >= ?", $date_start);
				$select->where("c.sign_date <= ?", $date_end);
				$select->where("c.status != ?", 7);
				$select->where("s.product_id = ?", $product_id);
				
				if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
				{
					$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				}
				
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					$contract_id_arr = array();
					
					foreach($rows as $row)
					{
						if(!in_array($row['contract_id'], $contract_id_arr))
						{
							$contract_id_arr[] = $row['contract_id'];
							
							$product_info['qiandan'] += $row['receivable_value'];
							$product_info['qiankuan'] += ($row['receivable_value'] - $row['received_value']);
							$product_info['xinqian'] += 1;
						}
					}
				}
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id"));
				$select->joinLeft("b_contract_sku as s", "s.contract_id=c.contract_id", array());
//				$select->where("c.start_date >= ?", $date_start);
				$select->where("s.product_id = ?", $product_id);
				
				if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
				{
					$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				}
				
				$select->where("c.contract_sid is not null or c.contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					$contract_id_arr = array();
					
					foreach($rows as $row)
					{
						if(!in_array($row['contract_id'], $contract_id_arr))
						{
							$contract_id_arr[] = $row['contract_id'];
						}
					}
					
					if(!empty($contract_id_arr))
					{
						$select = $dealers_db->select();
						$select->from("b_logs_contract as l", array("sum(value) as vl"));
						$select->where("l.ctime >= ?", $time_start);
						$select->where("l.ctime <= ?", $time_end);
						$select->where("l.contract_id IN (?)", $contract_id_arr);
						$row = $dealers_db->fetchRow($select);
						
						$product_info['huikuan'] = $row['vl'];
					}
				}
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id"));
				$select->joinLeft("b_contract_sku as s", "s.contract_id=c.contract_id", array());
				$select->where("c.expired_date >= ?", $date_start);
				$select->where("c.expired_date <= ?", $date_end);
				$select->where("c.status != ?", 7);
				$select->where("s.product_id = ?", $product_id);
				
				if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
				{
					$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				}
				
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					$contract_id_arr = array();
					
					foreach($rows as $row)
					{
						if(!in_array($row['contract_id'], $contract_id_arr))
						{
							$contract_id_arr[] = $row['contract_id'];
							
							$product_info['daoqi'] += 1;
						}
					}
				}
				
				$select = $dealers_db->select();
				$select->from("b_contract as c", array("contract_id"));
				$select->joinLeft("b_contract_sku as s", "s.contract_id=c.contract_id", array());
				$select->where("c.expired_date >= ?", $date_start);
				$select->where("s.product_id = ?", $product_id);
				$select->where("c.launch_switcher = ?", 2);
				
				if(in_array($_SESSION['identity']['level'], array("LEVEL-1", "LEVEL-2")))
				{
					$select->where("c.sales_emp_id IN (?)", $emp_id_arr);
				}
				
				$select->where("c.contract_sid is not null or contract_sid != ''");
				$rows = $dealers_db->fetchAll($select);
				
				if(!empty($rows))
				{
					$contract_id_arr = array();
					
					foreach($rows as $row)
					{
						if(!in_array($row['contract_id'], $contract_id_arr))
						{
							$contract_id_arr[] = $row['contract_id'];
							
							$product_info['zhixing'] += 1;
						}
					}
				}
				
				$result['products'][] = $product_info;
			}
		}
		
		return $result;
	}
	
	function GetClients()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
        
        $mod_params = new Db_Tables2_BDictParams();
        $sys_id = $mod_params->GetSysId();
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from("b_client", array("count(client_id) as ct"));
		
		if($this->provinceId)
		{
			$data->where("province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("client_name like '%".$this->keyword."%' or address like '%".$this->keyword."%' or tel like '%".$this->keyword."%' or cellphone like '%".$this->keyword."%'");
		}
		
		if("LEVEL-1" == $_SESSION['identity']['level'])
		{
			$data->where("emp_id = ?", $_SESSION['identity']['userid']);
		}

		$ct = $dealers_db->fetchRow($data);
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("dealers_db".$sys_id.".b_client as c", array("client_id", "client_name", "province_name", "city_name", "county_name", "tel", "cellphone", "status", "ctime"));
		$data->joinLeft("dealers_db".$sys_id.".b_client_type as t", "t.client_type_id=c.client_type_id", array("name as client_type_name"));
		$data->joinLeft("hxb_db".$sys_id.".auth_emp as e", "e.id=c.emp_id", array("realName as emp_name"));
		
		if($this->provinceId)
		{
			$data->where("c.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("c.city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("c.status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("c.client_name like '%".$this->keyword."%' or c.address like '%".$this->keyword."%' or c.tel like '%".$this->keyword."%' or c.cellphone like '%".$this->keyword."%'");
		}

		if("LEVEL-1" == $_SESSION['identity']['level'])
		{
			$data->where("c.emp_id = ?", $_SESSION['identity']['userid']);
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("c.client_id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"client_name" => array(),
				"province_name" => array(),
				"city_name" => array(),
				"county_name" => array(),
				"tel" => array(),
				"cellphone" => array(),
				"status" => array(),
				"ctime" => array(),
				"client_type_name" => array(),
				"emp_name" => array(),
				"next_leader_id" => array(),
				"next_leader_name" => array()
			);
			
			$mod_approval = new Db_Tables2_BLogsSalesApproval();
			
			foreach($origine as $row)
			{
				$current_page_info['client_name'][$row['client_id']] = $row['client_name'];
				$current_page_info['province_name'][$row['client_id']] = $row['province_name'];
				$current_page_info['city_name'][$row['client_id']] = $row['city_name'];
				$current_page_info['county_name'][$row['client_id']] = $row['county_name'];
				$current_page_info['tel'][$row['client_id']] = $row['tel'];
				$current_page_info['cellphone'][$row['client_id']] = $row['cellphone'];
				$current_page_info['status'][$row['client_id']] = $row['status'];
				$current_page_info['ctime'][$row['client_id']] = $row['ctime'];
				$current_page_info['client_type_name'][$row['client_id']] = $row['client_type_name'];
				$current_page_info['emp_name'][$row['client_id']] = $row['emp_name'];
				
				if(0 == $row['status'])
				{
					$next = $mod_approval->GetNext(1, $row['client_id']);
					
					if(!empty($next))
					{
						$current_page_info['next_leader_id'][$row['client_id']] = $next['emp_id'];
						$current_page_info['next_leader_name'][$row['client_id']] = $next['emp_name'];
					}
				}
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetClientSigner()
	{
		$result = array(
			'current_page_info' => array()
		);
		
		$dealers_db = Zend_Registry::get('dealers_db');
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("b_client_signer as b", array("id", "status"));
		$data->joinLeft("b_client as c", "c.client_id=b.client_id", array("client_name"));
		$data->joinLeft("b_signer as s", "s.signer_id=b.signer_id", array("company_name as signer_name"));
		$data->order(array("b.id DESC"));
		$data->limit(30);
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"client_name" => array(),
				"signer_name" => array(),
				"status" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['client_name'][$row['id']] = $row['client_name'];
				$current_page_info['signer_name'][$row['id']] = $row['signer_name'];
				$current_page_info['status'][$row['id']] = $row['status'];
			}
		}
		
		$result = array(
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetMaozhuaUsers()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$data = $maozhua_wb->select();
		$data->from("users", array("count(user_id) as ct"));


		if($this->provinceId)
		{
			$data->where("province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("user_name like '%".$this->keyword."%' or cellphone like '%".$this->keyword."%'");
		}
		
		$ct = $maozhua_wb->fetchRow($data);
		
		//dump all in condition
		$data = $maozhua_wb->select();
		$data->from("maozhua_wb.users as u", array("user_id", "user_name", "cellphone", "expired_date", "status","start_date","czht_dealer_id"));
		$data->joinLeft("maozhua_wb.t_area as a", "a.cityId=u.city_id", array("provinceName", "cityName"));
		$data->joinLeft("hxb_db.auth_emp as e", "e.id=u.emp_id", array("realName as emp_name"));
		if($this->provinceId)
		{
			$data->where("u.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("u.city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("u.status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%'");
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("u.status ASC", "u.user_id DESC"));
		
		$origine = $maozhua_wb->fetchAll($data);

		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"user_name" => array(),
				"cellphone" => array(),
				"expired_date" => array(),
				"status" => array(),
				"provinceName" => array(),
				"cityName" => array(),
				"emp_name" => array(),
                "start_date"=>array(),
                "czht_dealer_id"=>array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['user_name'][$row['user_id']] = $row['user_name'];
				$current_page_info['cellphone'][$row['user_id']] = $row['cellphone'];
				$current_page_info['expired_date'][$row['user_id']] = $row['expired_date'];
				$current_page_info['status'][$row['user_id']] = $row['status'];
				$current_page_info['provinceName'][$row['user_id']] = $row['provinceName'];
				$current_page_info['cityName'][$row['user_id']] = $row['cityName'];
				$current_page_info['emp_name'][$row['user_id']] = $row['emp_name'];
				$current_page_info['start_date'][$row['user_id']] = $row['start_date'];
				$current_page_info['czht_dealer_id'][$row['user_id']] = $row['czht_dealer_id'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetMaozhuaMainBrand()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		//count total for pagination
		$data_num = $maozhua_wb->select();
		$data_num->from("main_brand", array("count(id) as ct"));
		if($this->keyword)
		{
			$data_num->where("primary_factory_name like '%".$this->keyword."%' or dealer_name like '%".$this->keyword."%'");
		}
		$ct = $maozhua_wb->fetchRow($data_num);
		
		//select data
		$data = $maozhua_wb->select();
		$data->from("main_brand", array('*'));
		if($this->keyword)
		{
			$data->where("primary_factory_name like '%".$this->keyword."%' or dealer_name like '%".$this->keyword."%'");
		}
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		$data->order("id DESC");
		$origine = $maozhua_wb->fetchAll($data);

		$current_page_info = array();
		if(!empty($origine))
		{
			$current_page_info = $origine;
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		return $result;
	}

        
    function GetMaozhuaUsersExport()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$data = $maozhua_wb->select();
		$data->from("users", array("count(user_id) as ct"));


		if($this->provinceId)
		{
			$data->where("province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("user_name like '%".$this->keyword."%' or cellphone like '%".$this->keyword."%'");
		}
		
		$ct = $maozhua_wb->fetchRow($data);
		
		//dump all in condition
		$data = $maozhua_wb->select();
		$data->from("maozhua_wb.users as u", array("user_id", "user_name", "cellphone", "expired_date", "status","start_date","czht_dealer_id","contract_sid"));
		$data->joinLeft("maozhua_wb.t_area as a", "a.cityId=u.city_id", array("provinceName", "cityName"));
		$data->joinLeft("hxb_db.auth_emp as e", "e.id=u.emp_id", array("realName as emp_name"));
		if($this->provinceId)
		{
			$data->where("u.province_id = ?", $this->provinceId);
		}
		
		if($this->cityId)
		{
			$data->where("u.city_id = ?", $this->cityId);
		}
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status_arr = explode("_", $this->status_str);
			$data->where("u.status IN (?)", $status_arr);
		}
		
		if($this->keyword)
		{
			$data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%'");
		}
		
		$data->order(array("u.status ASC", "u.user_id DESC"));
		
		$origine = $maozhua_wb->fetchAll($data);

		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"user_name" => array(),
				"cellphone" => array(),
				"expired_date" => array(),
				"status" => array(),
				"provinceName" => array(),
				"cityName" => array(),
				"emp_name" => array(),
                "start_date"=>array(),
                "czht_dealer_id"=>array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['user_name'][$row['user_id']] = $row['user_name'];
				$current_page_info['cellphone'][$row['user_id']] = $row['cellphone'];
				$current_page_info['expired_date'][$row['user_id']] = $row['expired_date'];
				$current_page_info['status'][$row['user_id']] = $row['status'];
				$current_page_info['provinceName'][$row['user_id']] = $row['provinceName'];
				$current_page_info['cityName'][$row['user_id']] = $row['cityName'];
				$current_page_info['emp_name'][$row['user_id']] = $row['emp_name'];
				$current_page_info['start_date'][$row['user_id']] = $row['start_date'];
				$current_page_info['czht_dealer_id'][$row['user_id']] = $row['czht_dealer_id'];
                                $current_page_info['contract_sid'][$row['user_id']] = $row['contract_sid'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
        
    function GetMaozhualogs()
    {
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');

        $data = $maozhua_wb->select();
        $data->from("log1", array("count(id) as ct"));


        if($this->keyword)
        {
            $data->where("contents like '%".$this->keyword."%'");
        }

        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }
        if($this->date_start)
        {
            $data->where("ctime >= ?", $date_start);
            $data->where("ctime <= ?", $date_end);
        }

        $ct = $maozhua_wb->fetchRow($data);

        //dump all in condition
        $data = $maozhua_wb->select();
        $data->from("maozhua_wb.log1", array("id", "contents", "ctime"));



        if($this->date_start)
        {
            $data->where("ctime >= ?", $date_start);
            $data->where("ctime <= ?", $date_end);
        }

        if($this->keyword)
        {
            $data->where("contents like '%".$this->keyword."%'");
        }

        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }

        $data->order(array("ctime DESC"));

        $origine = $maozhua_wb->fetchAll($data);

        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "ctime" => array(),
                "contents" => array(),

            );

            foreach($origine as $row)
            {
                $current_page_info['ctime'][$row['id']] = $row['ctime'];
                $current_page_info['contents'][$row['id']] = $row['contents'];
            }
        }

        $result = array(
            'ct' => $ct['ct'],
            'total_pages' => ceil($ct['ct'] / $this->page_size),
            'current_page_info' => $current_page_info
        );

        return $result;
    }

    function GetMaozhuaTaskLogs()
    {
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');


        $data = $maozhua_wb->select();
        $data->from("logs_task as l", array("count(id) as ct","task_id", "account_id"));
        $data->joinLeft("accounts as a", "a.account_id=l.account_id", array("user_id"));
        $data->joinLeft("users as u", "u.user_id=a.user_id", array("user_name","cellphone"));


        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }

        if($this->date_start)
        {
            $data->where("l.db_time >= ?", $date_start);
            $data->where("l.db_time <= ?", $date_end);
        }
        
        if(is_numeric($this->status))
        {
            $data->where("l.status = ?",$this->status);
        }

        if(!empty($this->keyword))
        {
            $data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%' or l.task_id = '".$this->keyword."'");
        }
        $ct = $maozhua_wb->fetchRow($data);

        //dump all in condition
        $data = $maozhua_wb->select();
        $data->from("logs_task as l", array("id","task_id", "status", "error_msg", "db_time", "is_noticed", "is_recommended", "series_name", "run_hour", "run_min", "run_sec", "topic_id", "topic_name"));
        $data->joinLeft("accounts as a", "a.account_id=l.account_id", array("dealer_id"));
        $data->joinLeft("users as u", "u.user_id=a.user_id", array("user_name","cellphone"));

        if($this->date_start)
        {
            $data->where("l.db_time >= ?", $date_start);
            $data->where("l.db_time <= ?", $date_end);
        }

        if($this->task_id)
        {
            $data->where("l.task_id = ?", $this->task_id);
        }

        if($this->error_msg)
        {
            $data->where("l.error_msg = ?", $this->error_msg);
        }


        if($this->keyword)
        {
            $data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%' or l.task_id = '".$this->keyword."'");

        }
        
        if(is_numeric($this->status))
        {
            $data->where("l.status = ?",$this->status);
        }
        
        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }

        $data->order(array("l.id DESC"));

        $origine = $maozhua_wb->fetchAll($data);


        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "user_name" => array(),
                "task_id" => array(),
                "error_msg" => array(),
                "status" => array(),
                "is_recommended" => array(),
                "series_name" => array(),
                "run_min" => array(),
                "run_sec" => array(),
                "db_time" => array(),
                "cellphone" => array(),
                "is_noticed" => array(),
                "run_hour" => array(),
                "topic_id" => array(),
                "topic_name" => array(),
                "dealer_id" => array()
            );

            foreach($origine as $row)
            {
                $current_page_info['user_name'][$row['id']] = $row['user_name'];
                $current_page_info['task_id'][$row['id']] = $row['task_id'];
                $current_page_info['error_msg'][$row['id']] = $row['error_msg'];
                $current_page_info['status'][$row['id']] = $row['status'];
                $current_page_info['series_name'][$row['id']] = $row['series_name'];
                $current_page_info['is_recommended'][$row['id']] = $row['is_recommended'];
                $current_page_info['run_min'][$row['id']] = $row['run_min'];
                $current_page_info['run_sec'][$row['id']] = $row['run_sec'];
                $current_page_info['db_time'][$row['id']] = $row['db_time'];
                $current_page_info['cellphone'][$row['id']] = $row['cellphone'];
                $current_page_info['is_noticed'][$row['id']] = $row['is_noticed'];
                $current_page_info['run_hour'][$row['id']] = $row['run_hour'];
                $current_page_info['topic_id'][$row['id']] = $row['topic_id'];
                $current_page_info['dealer_id'][$row['id']] = $row['dealer_id'];
                $current_page_info['topic_name'][$row['id']] = $row['topic_name'];
            }
        }

        $result = array(
            'ct' => $ct['ct'],
            'total_pages' => ceil($ct['ct'] / $this->page_size),
            'current_page_info' => $current_page_info
        );

        return $result;
    }

    function GetMaozhuaTaskLogs2()
    {
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');


        $data = $maozhua_wb->select();
        $data->from("yc_logs_task as l", array("count(id) as ct","task_id"));
        $data->joinLeft("yc_tasks as t", "t.task_id=l.task_id", array("account_id"));
        $data->joinLeft("accounts as a", "a.account_id=t.account_id", array("user_id"));
        $data->joinLeft("users as u", "u.user_id=a.user_id", array("user_name","cellphone"));


        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }

        if($this->date_start)
        {
            $data->where("l.ctime >= ?", $date_start);
            $data->where("l.ctime <= ?", $date_end);
        }
        
        if(is_numeric($this->status))
        {
            $data->where("l.status = ?",$this->status);
        }

        if(!empty($this->keyword))
        {
            $data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%'");
        }

        $ct = $maozhua_wb->fetchRow($data);

        //dump all in condition

		$data = $maozhua_wb->select();
        $data->from("yc_logs_task as l", array("id as log_id", "status", "error_msg", "ctime", "remark"));
        $data->joinLeft("yc_tasks as t", "t.task_id=l.task_id", array());
        $data->joinLeft("accounts as a", "a.account_id=t.account_id", array());
        $data->joinLeft("users as u", "u.user_id=a.user_id", array("user_name","cellphone"));

        if($this->date_start)
        {
            $data->where("l.ctime >= ?", $date_start);
            $data->where("l.ctime <= ?", $date_end);
        }

        if($this->error_msg)
        {
            $data->where("l.error_msg = ?", $this->error_msg);
        }


        if($this->keyword)
        {
            $data->where("u.user_name like '%".$this->keyword."%' or u.cellphone like '%".$this->keyword."%' or l.id = '".$this->keyword."'");

        }

        if(is_numeric($this->status))
        {
            $data->where("l.status = ?",$this->status);
        }
        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }

        $data->order(array("l.ctime DESC"));
//die;
//        var_dump($maozhua_wb->fetchAll($data));die;
        $origine = $maozhua_wb->fetchAll($data);


        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "log_id" => array(),
                "status" => array(),
                "error_msg" => array(),
                "ctime" => array(),
                "remark" => array(),
                "cellphone" => array(),
                "user_name" => array()
            );

            foreach($origine as $row)
            {
                $current_page_info['log_id'][$row['log_id']] = $row['log_id'];
                $current_page_info['error_msg'][$row['log_id']] = $row['error_msg'];
                $current_page_info['ctime'][$row['log_id']] = $row['ctime'];
                $current_page_info['user_name'][$row['log_id']] = $row['user_name'];
                $current_page_info['status'][$row['log_id']] = $row['status'];
                $current_page_info['cellphone'][$row['log_id']] = $row['cellphone'];
                $current_page_info['remark'][$row['log_id']] = $row['remark'];
            }
        }

        $result = array(
            'ct' => $ct['ct'],
            'total_pages' => ceil($ct['ct'] / $this->page_size),
            'current_page_info' => $current_page_info
        );

        return $result;
    }

    function GetMaozhuaAppIcs()
    {
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');

        //dump all in condition

        $data = $maozhua_wb->select();
        $data->from("accounts as a", array(""));
        $data->joinLeft("users as u", "u.user_id=a.user_id", array("user_name"));
        $data->joinLeft("t_icsapp_login_log as l", "l.userId=u.user_id", array("id","loginTime", "msg"));
        $data->where("a.status = ?", 1);
        $data->where("u.status = ?", 1);
        $data->where("a.account_type = ?", 4);
        if($this->date_start or $this->date_end)
        {
            if($this->date_start)
            {
                $data->where("l.loginTime >= ?",$this->date_start." 00:00:00");
            }
            if($this->date_end)
            {
                $data->where("l.loginTime <= ?",$this->date_end." 23:59:59");
            }
        }else{
            $data->where("l.loginTime >= ?", date("Y-m-d")." 00:00:00");
        }

        if($this->keyword)
        {
            $data->where("u.user_name like '%$this->keyword%'");
        }

        if(is_numeric($this->status))
        {
            $data->where("l.state = ?",$this->status);
        }


        $ct = count($maozhua_wb->fetchAll($data));
        $ct = array("ct" => $ct);



        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }

        $data->order(array("l.loginTime DESC"));
//die;
        $origine = $maozhua_wb->fetchAll($data);


        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "user_name" => array(),
                "id" => array(),
                "loginTime" => array(),
                "msg" => array()
            );

            foreach($origine as $row)
            {
                $current_page_info['user_name'][$row['id']] = $row['user_name'];
                $current_page_info['id'][$row['id']] = $row['id'];
                $current_page_info['loginTime'][$row['id']] = $row['loginTime'];
                $current_page_info['msg'][$row['id']] = $row['msg'];
            }
        }

        $result = array(
            'ct' => $ct['ct'],
            'total_pages' => ceil($ct['ct'] / $this->page_size),
            'current_page_info' => $current_page_info
        );

        return $result;
    }
	
	function GetMaozhuaIndexData($type)
    { 
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');

        $data = $maozhua_wb->select();
        $data->from("logs_index_data", array("count(log_id) as ct"));


        if($this->keyword)
        {
            $keyword = trim($this->keyword);
        }

        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }
		
		if($type){
			$log_ids=$this->GetLogIds(1);
		}
		
        if(is_numeric($this->status_zj))
        {
            $data->where("status_zj = ?",$this->status_zj);
        }
        
        if(is_numeric($this->status_yc))
        {
            $data->where("status_yc = ?",$this->status_yc);
        }

        if(is_numeric($this->zj_manfen))
        {
            $data->where("zj_manfen = ?",$this->zj_manfen);
        }
        
        if(is_numeric($this->yc_manfen))
        {
            $data->where("yc_manfen = ?",$this->yc_manfen);
        }
        $ct = $maozhua_wb->fetchRow($data);
        //dump all in condition
        $data = $maozhua_wb->select();
        $data->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime"));
        $data->joinLeft("users as u", "u.user_id=l.user_id", array("user_name"));

        if(!empty($this->date_start))
        {
            $data->where("ctime >= ?", $date_start);
            $data->where("ctime <= ?", $date_end);
        }

        if($this->keyword)
        {
            $data->where("u.user_name like '%".$keyword."%' or u.cellphone like '%".$keyword."%'");

        }
        
        if(is_numeric($this->status_zj))
        {
            $data->where("l.status_zj = ?",$this->status_zj);
        }
        
        if(is_numeric($this->status_yc))
        {
            $data->where("l.status_yc = ?",$this->status_yc);
        }
        
        if(is_numeric($this->zj_manfen))
        {
            $data->where("zj_manfen = ?",$this->zj_manfen);
        }
        
        if(is_numeric($this->yc_manfen))
        {
            $data->where("yc_manfen = ?",$this->yc_manfen);
        }
        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }

        $data->order(array("l.log_id DESC"));
		if($type){
			$data->where("l.log_id in (?)", $log_ids); 
		}
		
        $origine = $maozhua_wb->fetchAll($data);
//        var_dump($origine);die;

        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "log_id" => array(),
                "status_zj" => array(),
                "status_yc" => array(),
                "data_zj" => array(),
                "data_yc" => array(),
                "ctime" => array(),
                "user_name" => array()
            );

            foreach($origine as $row)
            {
                $current_page_info['ctime'][$row['log_id']] = $row['ctime'];
                $current_page_info['status_zj'][$row['log_id']] = $row['status_zj'];
                $current_page_info['log_id'][$row['log_id']] = $row['log_id'];
                $current_page_info['status_yc'][$row['log_id']] = $row['status_yc'];
                $current_page_info['data_zj'][$row['log_id']] = $row['data_zj'];
                $current_page_info['data_yc'][$row['log_id']] = $row['data_yc'];
                $current_page_info['user_name'][$row['log_id']] = $row['user_name'];
            }
        }

        $result = array(
            'ct' => $ct['ct'],
            'total_pages' => ceil($ct['ct'] / $this->page_size),
            'current_page_info' => $current_page_info
        );
//        var_dump($result);die;
        return $result;
    }
	
    function GetMaozhuaIndexZjData($type)
    { 
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
		$user_data = $maozhua_wb->select();
		$user_data->from("accounts as a", array('city_id', 'dealer_id'));
		$user_data->joinLeft("users as u", "a.user_id=u.user_id", array("user_id", "user_name"));	
		//$user_data->where("u.status = ?", 1);
		$user_data->where("a.account_type = ?", $type);		 
		$data_array = $maozhua_wb->fetchAll($user_data);
		 
		foreach($data_array as $key => $value)
		{
			$user_ids[] = $value["user_id"];
		}
		//echo count($data_array);die;
		 if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }
        $data = $maozhua_wb->select();
        $data->from("logs_index_data as l", array("max(log_id) as max_id"));
		$data->where("user_id in (?)", $user_ids);
		if(!empty($this->date_start))
        {
            $data->where("l.ctime >= ?", $date_start);
            $data->where("l.ctime <= ?", $date_end);
        }
		$data->group("user_id");		
		$data_maxids = $maozhua_wb->fetchAll($data);
		foreach($data_maxids as $key => $value)
		{
			$log_ids[] = $value["max_id"];
		}
		

		$data = $maozhua_wb->select();
        $data->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime"));
		$data->joinLeft("accounts as a", "l.account_id_zj=a.account_id", array("status"));	
		$data->joinLeft("users as u", "l.user_id=u.user_id", array("user_id", "user_name"));	
		$data->where("l.log_id in (?)", $log_ids); 

        if($this->keyword)
        {
            $keyword = trim($this->keyword);
        }

       
        if(is_numeric($this->status_zj))
        {
            $data->where("status_zj = ?",$this->status_zj);
        }
		if($this->keyword)
        {
           $data->where("u.user_name like '%".$keyword."%' or u.cellphone like '%".$keyword."%'");

        }
        
        if(is_numeric($this->status_yc))
        {
            $data->where("status_yc = ?",$this->status_yc);
        }

        if(is_numeric($this->zj_manfen))
        {
            $data->where("zj_manfen = ?",$this->zj_manfen);
        }
		if(is_numeric($this->account_status))
        {
            $data->where("a.status = ?",$this->account_status);
        }
        
        if(is_numeric($this->yc_manfen))
        {
            $data->where("yc_manfen = ?",$this->yc_manfen);
        }			 
		$data->order(array("l.log_id DESC"));	
		 
        $ct = $maozhua_wb->fetchAll($data);
		
		if($type == 1){
			//查询APP登录的实际情况
			$data_app = $maozhua_wb->select();
			$data_app->from("t_icsapp_login_log", array("max(id) as max_id","userId"));
			$data_app->where("userId in (?)", $user_ids);
			$data_app->group("userId");		
			$data_app_maxids = $maozhua_wb->fetchAll($data_app);
			foreach($data_app_maxids as $key => $value)
			{
				$app_ids[] = $value["max_id"];
			}

			$data_app = $maozhua_wb->select();
			$data_app->from("t_icsapp_login_log as t", array("state", "userId"));
			$data_app->joinLeft("logs_index_data as l", "t.userId=l.user_id", array());	
			$data_app->where("t.id in (?)", $app_ids);
			$data_app->where("t.loginTime >= ?", date("Y-m-d 00:00:00"));
			$state=$maozhua_wb->fetchAll($data_app);

			$app_status=array();
			foreach($state as $key => $value)
			{
				$app_status[$value['userId']] = $value["state"];
			}
			
			//dump all in condition
			$data = $maozhua_wb->select();
			$data->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime","user_id"));
			$data->joinLeft("accounts as a", "l.account_id_zj=a.account_id", array("status"));	
			$data->joinLeft("users as u", "l.user_id=u.user_id", array("user_id", "user_name"));
			$data->where("l.log_id in (?)", $log_ids); 
		}elseif($type == 2){
			//查询APP登录的实际情况
			$data_app = $maozhua_wb->select();
			$data_app->from("t_ycsapp_login_log", array("max(id) as max_id","userId"));
			$data_app->where("userId in (?)", $user_ids);
			$data_app->group("userId");		
			$data_app_maxids = $maozhua_wb->fetchAll($data_app);
			foreach($data_app_maxids as $key => $value)
			{
				$app_ids[] = $value["max_id"];
			}

			$data_app = $maozhua_wb->select();
			$data_app->from("t_ycsapp_login_log as t", array("state", "userId"));
			$data_app->joinLeft("logs_index_data as l", "t.userId=l.user_id", array());	
			$data_app->where("t.id in (?)", $app_ids);
			$data_app->where("t.loginTime >= ?", date("Y-m-d 00:00:00"));
			$state=$maozhua_wb->fetchAll($data_app);

			$app_status=array();
			foreach($state as $key => $value)
			{
				$app_status[$value['userId']] = $value["state"];
			}
			
			//dump all in condition
			$data = $maozhua_wb->select();
			$data->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime","user_id"));
			$data->joinLeft("accounts as a", "l.account_id_zj=a.account_id", array("status"));	
			$data->joinLeft("users as u", "l.user_id=u.user_id", array("user_id", "user_name"));
			$data->where("l.log_id in (?)", $log_ids); 
		}

        if($this->keyword)
        {
           $data->where("u.user_name like '%".$keyword."%' or u.cellphone like '%".$keyword."%'");
        }
		
        if(is_numeric($this->account_status))
        {
            $data->where("a.status = ?",$this->account_status);
        }
        if(is_numeric($this->status_zj))
        {
            $data->where("l.status_zj = ?",$this->status_zj);
        }
        
        if(is_numeric($this->status_yc))
        {
            $data->where("l.status_yc = ?",$this->status_yc);
        }
        
        if(is_numeric($this->zj_manfen))
        {
            $data->where("zj_manfen = ?",$this->zj_manfen);
        }
        
        if(is_numeric($this->yc_manfen))
        {
            $data->where("yc_manfen = ?",$this->yc_manfen);
        }
		 
		
        if($this->page_id && !$this->skip_pagination)
        {
            $data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
        }
        $data->order(array("l.log_id DESC"));
        $origine = $maozhua_wb->fetchAll($data);
		 
        $current_page_info = array();

        if(!empty($origine))
        {
            $current_page_info = array(
                "log_id" => array(),
                "status_zj" => array(),
                "status_yc" => array(),
                "data_zj" => array(),
                "data_yc" => array(),
                "ctime" => array(),
                "user_name" => array(),
				"status" => array(),
//				"app_status" => array()
            );

            foreach($origine as $row)
            {
                $current_page_info['ctime'][$row['log_id']] = $row['ctime'];
                $current_page_info['status_zj'][$row['log_id']] = $row['status_zj'];
                $current_page_info['log_id'][$row['log_id']] = $row['log_id'];
                $current_page_info['status_yc'][$row['log_id']] = $row['status_yc'];
                $current_page_info['data_zj'][$row['log_id']] = $row['data_zj'];
                $current_page_info['data_yc'][$row['log_id']] = $row['data_yc'];
                $current_page_info['user_name'][$row['log_id']] = $row['user_name'];
			    $current_page_info['status'][$row['log_id']] = $row['status'];
				$current_page_info['app_status'][$row['log_id']] = $app_status[$row['user_id']];
            }
        }

        $result = array(
            'ct' => count($ct),
            'total_pages' => ceil(count($ct) / $this->page_size),
            'current_page_info' => $current_page_info
        );
//        var_dump($result['ct']);die;
        return $result;
    }

    function GetDccCompanies()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$data = $maozhua_wb->select();
		$data->from("dcc_companies", array("count(company_id) as ct"));
		
		if($this->keyword)
		{
			$data->where("company_name like '%".$this->keyword."%'");
		}
		
		if($this->staff_name)
		{
			$data->where("staff_name like '%".$this->staff_name."%'");
		}
		
		$ct = $maozhua_wb->fetchRow($data);
		
		//dump all in condition
		$data = $maozhua_wb->select();
		$data->from("dcc_companies", "*");
		
		if($this->keyword)
		{
			$data->where("company_name like '%".$this->keyword."%'");
		}
		
		if($this->staff_name)
		{
			$data->where("staff_name like '%".$this->staff_name."%'");
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("company_id DESC"));
		
		$origine = $maozhua_wb->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"company_name" => array(),
				"balance1" => array(),
				"balance2" => array(),
				"status" => array(),
				"total_users" => array(),
				"history_call_in_mins" => array(),
				"history_call_out_mins" => array(),
				"history_fee" => array(),
				"staff_name" => array(),
                "czht_dealer_id"=>array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['company_name'][$row['company_id']] = $row['company_name'];
				$current_page_info['balance1'][$row['company_id']] = $row['balance1'];
				$current_page_info['balance2'][$row['company_id']] = $row['balance2'];
				$current_page_info['status'][$row['company_id']] = $row['status'];
				$current_page_info['total_users'][$row['company_id']] = $row['total_users'];
				$current_page_info['history_call_in_mins'][$row['company_id']] = $row['history_call_in_mins'];
				$current_page_info['history_call_out_mins'][$row['company_id']] = $row['history_call_out_mins'];
				$current_page_info['history_fee'][$row['company_id']] = $row['history_fee'];
				$current_page_info['staff_name'][$row['company_id']] = $row['staff_name'];
                $current_page_info['czht_dealer_id'][$row['company_id']] = $row['czht_dealer_id'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetInvoices()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from("b_contract_invoice", array("count(id) as ct"));
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status = explode("_", $this->status_str);
			$data->where("status IN (?)", $status);
		}
		
		if($this->sales_emp_id)
		{
			$data->where("sales_emp_id = ?", $this->sales_emp_id);
		}
		
		if($this->keyword)
		{
			$data->where("contract_sid like '%".$this->keyword."%' or company_name like '%".$this->keyword."%' or tax_number like '%".$this->keyword."%' or register_address like '%".$this->keyword."%' or bank_name like '%".$this->keyword."%' or contact_name like '%".$this->keyword."%' or tel like '%".$this->keyword."%' or contact_address like '%".$this->keyword."%' or sales_emp_name like '%".$this->keyword."%' or invoice_num like '%".$this->keyword."%' or carrier_tracking_num like '%".$this->keyword."%'");
		}
		
		$ct = $dealers_db->fetchRow($data);
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("b_contract_invoice", "*");
		
		if($this->status_str || is_numeric($this->status_str))
		{
			$status = explode("_", $this->status_str);
			$data->where("status IN (?)", $status);
		}
		
		if($this->sales_emp_id)
		{
			$data->where("sales_emp_id = ?", $this->sales_emp_id);
		}
		
		if($this->keyword)
		{
			$data->where("contract_sid like '%".$this->keyword."%' or company_name like '%".$this->keyword."%' or tax_number like '%".$this->keyword."%' or register_address like '%".$this->keyword."%' or bank_name like '%".$this->keyword."%' or contact_name like '%".$this->keyword."%' or tel like '%".$this->keyword."%' or contact_address like '%".$this->keyword."%' or sales_emp_name like '%".$this->keyword."%' or invoice_num like '%".$this->keyword."%' or carrier_tracking_num like '%".$this->keyword."%'");
		}
		
		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		$data->order(array("id DESC"));
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{
			$current_page_info = array(
				"contract_sid" => array(),
				"invoice_type" => array(),
				"company_name" => array(),
				"tax_number" => array(),
				"register_address" => array(),
				"bank_name" => array(),
				"bank_number" => array(),
				"contact_name" => array(),
				"tel" => array(),
				"contact_address" => array(),
				"img1" => array(),
				"img3" => array(),
				"img4" => array(),
				"sales_emp_name" => array(),
				"status" => array(),
				"ctime" => array(),
				"invoice_num" => array(),
				"carrier_id" => array(),
				"carrier_name" => array(),
				"carrier_tracking_num" => array(),
				"end_time" => array(),
				"invoice_amount" => array()
			);
			
			$mod_invoice_sku = new Db_Tables2_BContractInvoiceSku();
			
			foreach($origine as $row)
			{
				$current_page_info['contract_sid'][$row['id']] = $row['contract_sid'];
				$current_page_info['invoice_type'][$row['id']] = $row['invoice_type'];
				$current_page_info['company_name'][$row['id']] = $row['company_name'];
				$current_page_info['tax_number'][$row['id']] = $row['tax_number'];
				$current_page_info['register_address'][$row['id']] = $row['register_address'];
				$current_page_info['bank_name'][$row['id']] = $row['bank_name'];
				$current_page_info['bank_number'][$row['id']] = $row['bank_number'];
				$current_page_info['contact_name'][$row['id']] = $row['contact_name'];
				$current_page_info['tel'][$row['id']] = $row['tel'];
				$current_page_info['contact_address'][$row['id']] = $row['contact_address'];
				$current_page_info['img1'][$row['id']] = $row['img1'];
				$current_page_info['img3'][$row['id']] = $row['img3'];
				$current_page_info['img4'][$row['id']] = $row['img4'];
				$current_page_info['sales_emp_name'][$row['id']] = $row['sales_emp_name'];
				$current_page_info['status'][$row['id']] = $row['status'];
				$current_page_info['ctime'][$row['id']] = $row['ctime'];
				$current_page_info['invoice_num'][$row['id']] = $row['invoice_num'];
				$current_page_info['carrier_id'][$row['id']] = $row['carrier_id'];
				$current_page_info['carrier_name'][$row['id']] = $row['carrier_name'];
				$current_page_info['carrier_tracking_num'][$row['id']] = $row['carrier_tracking_num'];
				$current_page_info['end_time'][$row['id']] = $row['end_time'];
				$current_page_info['invoice_amount'][$row['id']] = $mod_invoice_sku->GetInvoiceAmount($row['id']);
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
		
		return $result;
	}
	
	function GetMaozhuaAppUsersListData()
    { 
		if($this->page_id)
        {
            $page_id = $this->page_id;
        }
		if($this->keyword)
        {
            $keyword = trim($this->keyword);
        }
		
		//查询数据总条数
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		$count = $maozhua_wb->select();
		$count->from("app_ics_account as a", array("count(*) as total"));
		$count->joinLeft("maozhua_wb.users as u", "a.user_id=u.user_id", array(""));
		if($this->keyword)
        {
           $count->where("u.user_name like '%".$keyword."%' or a.cellphone like '%".$keyword."%'or a.cellphone like '%".$keyword."%' or a.user_name like '%".$keyword."%'");
        }
		if($this->status==1){
			$count->where("a.status = ?",1);
		}elseif($this->status==2){
			$count->where("a.status = ?",0);
		}
		$count_data = $maozhua_wb->fetchAll($count);
	
		$select = $maozhua_wb->select();
		$select->from("app_ics_account as a", array("user_name","cellphone","ctime","status"));
		$select->joinLeft("maozhua_wb.users as u", "a.user_id=u.user_id", array("user_name as company"));
		
		if($this->keyword)
        {
           $select->where("u.user_name like '%".$keyword."%' or a.cellphone like '%".$keyword."%'or a.cellphone like '%".$keyword."%' or a.user_name like '%".$keyword."%'");
        }
		if($this->status==1){
			$select->where("a.status = ?",1);
		}elseif($this->status==2){
			$select->where("a.status = ?",0);
		}
		$select->limit($this->page_size, $this->page_size * ($page_id - 1));
		$select->order(array("company desc"));
		$data = $maozhua_wb->fetchAll($select);
		
		$total=(int)$count_data[0]['total'];
		$total_pages=ceil($total / $this->page_size);
		
        $result = array(
            'total' => $total,
			'total_pages' => $total_pages,
            'data' => $data
        );
//        var_dump($result['ct']);die;
        return $result;
    }
	
	function GetMaozhuaAppUsersListExport()
    { 
		if($this->keyword)
        {
            $keyword = trim($this->keyword);
        }
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		$select = $maozhua_wb->select();
		$select->from("app_ics_account as a", array("user_name","cellphone","ctime","status"));
		$select->joinLeft("maozhua_wb.users as u", "a.user_id=u.user_id", array("user_name as company"));
		
		if($this->keyword)
        {
           $select->where("u.user_name like '%".$keyword."%' or a.cellphone like '%".$keyword."%'or a.cellphone like '%".$keyword."%' or a.user_name like '%".$keyword."%'");
        }
		if($this->status==1){
			$select->where("a.status = ?",1);
		}elseif($this->status==2){
			$select->where("a.status = ?",0);
		}
		$data = $maozhua_wb->fetchAll($select);
        return $data;
    }
	
	function GetEverydayDealerData($type)
    { 
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );
		
		if($this->city)
        {
            $city = trim($this->city);
        }
		
		if($this->user_name)
        {
            $user_name = trim($this->user_name);
        }

        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$select_count = $maozhua_wb->select();
		$select_count->from("logs_everyday_dealer_data", array("count(*) as total"));
		if(!empty($this->date_start))
        {
            $select_count->where("date >= ?", $date_start);
            $select_count->where("date <= ?", $date_end);
        }
		
        if($this->user_name || $this->city)
        {
            $select_count->where("user_name like '%".$user_name."%' and city_name like '%".$city."%'");
        }
        
        if(is_numeric($this->pingtai))
        {
            $select_count->where("account_type = ?",$this->pingtai);
		}
		
        $count = $maozhua_wb->fetchRow($select_count);
		
		$ct=(int)$count['total'];
		
		$select = $maozhua_wb->select();
		$select->from("logs_everyday_dealer_data", array("*","DATE_FORMAT(date,'%Y/%m/%d') as datetime","DATE_FORMAT(first_lost_time,'%Y/%m/%d') as f_lost_time","DATE_FORMAT(service_start_time,'%Y/%m/%d') as s_start_time","DATE_FORMAT(service_end_time,'%Y/%m/%d') as s_end_time"));
		if(!empty($this->date_start))
        {
            $select->where("date >= ?", $date_start);
            $select->where("date <= ?", $date_end);
        }
        if($this->user_name || $this->city)
        {
            $select->where("user_name like '%".$user_name."%' and city_name like '%".$city."%'");
        }
        if(is_numeric($this->pingtai))
        {
            $select->where("account_type = ?",$this->pingtai);
        }
		if(!$type){
			if($this->page_id && !$this->skip_pagination)
			{
	            $select->limit($this->page_size, $this->page_size * ($this->page_id - 1));
			}
		}
		$select->order("id desc");
        $data = $maozhua_wb->fetchAll($select);
		
        $result = array(
            'ct' => $ct,
            'total_pages' => ceil($ct / $this->page_size),
            'current_page_info' => $data,
        );

        return $result;
    }
	
	function GetEverydayDealerMonitor($type)
    { 
        $result = array(
            'total_pages' => 0,
            'current_page_info' => array()
        );
		
		if($this->city)
        {
            $city = trim($this->city);
        }
		
		if($this->user_name)
        {
            $user_name = trim($this->user_name);
        }

        if(!empty($this->date_start))
        {
            $date_start = $this->date_start." 00:00:00";
            if(!empty($this->date_end))
            {
                $date_end = $this->date_end." 23:59:59";
            }else{
                $date_end = $this->date_start." 23:59:59";
            }
        }

        //count total for pagination
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$select_count = $maozhua_wb->select();
		$select_count->from("logs_everyday_dealer_monitor", array("count(*) as total"));
		if(!empty($this->date_start))
        {
            $select_count->where("date >= ?", $date_start);
            $select_count->where("date <= ?", $date_end);
        }
		
        if($this->user_name || $this->city)
        {
            $select_count->where("user_name like '%".$user_name."%' and city_name like '%".$city."%'");
        }
        
        if(is_numeric($this->pingtai))
        {
            $select_count->where("account_type = ?",$this->pingtai);
		}
		
        $count = $maozhua_wb->fetchRow($select_count);
		
		$ct=(int)$count['total'];

		$select = $maozhua_wb->select();
		$select->from("logs_everyday_dealer_monitor", array("*","DATE_FORMAT(date,'%Y/%m/%d') as datetime"));
		if(!empty($this->date_start))
        {
            $select->where("date >= ?", $date_start);
            $select->where("date <= ?", $date_end);
        }
        if($this->user_name || $this->city)
        {
            $select->where("user_name like '%".$user_name."%' and city_name like '%".$city."%'");
        }
        if(is_numeric($this->pingtai))
        {
            $select->where("account_type = ?",$this->pingtai);
        }
		if(!$type){
			if($this->page_id && !$this->skip_pagination)
			{
	            $select->limit($this->page_size, $this->page_size * ($this->page_id - 1));
			}
		}
		$select->order("id desc");
        $data = $maozhua_wb->fetchAll($select);
		
        $result = array(
            'ct' => $ct,
            'total_pages' => ceil($ct / $this->page_size),
            'current_page_info' => $data,
        );

        return $result;
    }
	
	function GetLogIds($status){
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		$user_data = $maozhua_wb->select();
		$user_data->from("accounts as a", array('city_id', 'dealer_id'));
		$user_data->joinLeft("users as u", "a.user_id=u.user_id", array("user_id", "user_name"));
		if($status){
			$user_data->where("u.status = ?",1);
		}
		$data_array = $maozhua_wb->fetchAll($user_data);

		foreach($data_array as $key => $value)
		{
			$user_ids[] = $value["user_id"];
		}

		$data = $maozhua_wb->select();
		$data->from("logs_index_data as l", array("max(log_id) as max_id"));
		$data->where("user_id in (?)", $user_ids);
		if(!empty($this->date_start))
		{
			$data->where("l.ctime >= ?", $date_start);
			$data->where("l.ctime <= ?", $date_end);
		}
		$data->group("user_id");		
		$data_maxids = $maozhua_wb->fetchAll($data);
		foreach($data_maxids as $key => $value)
		{
			$log_ids[] = $value["max_id"];
		}
		return $log_ids;
	}
	
	function GetPayPlans()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		//count total for pagination
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$data = $dealers_db->select();
		$data->from("b_logs_contract_pay_plan as p", array("count(*) as ct", "sum(plan_value) as sm"));
		$data->joinLeft("b_contract as c", "c.contract_id=p.contract_id", array());
		$data->joinLeft("b_contract_sku as s", "s.contract_id=c.contract_id", array());
		
		if($this->date_start)
		{
			$data->where("p.plan_date >= ?", $this->date_start);
		}
		
		if($this->date_end)
		{
			$data->where("p.plan_date <= ?", $this->date_end);
		}
		
		if(!$this->date_start && !$this->date_end && $this->period_id)
		{
			if(1 == $this->period_id) //今天
			{
				$data->where("p.plan_date = ?", date("Y-m-d"));
			}elseif(2 == $this->period_id) //明天
			{
				$tmr = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+1,date("Y")));
				$data->where("p.plan_date = ?", $tmr);
			}elseif(3 == $this->period_id) //本周
			{
				$period_from = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(4 == $this->period_id) //下周
			{
				$period_from = date('Y-m-d', (time() + (7 - ((date('w') == 0 ? 7 : date('w')) - 1)) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 + (7 - (date('w') == 0 ? 7 : date('w')))) * 24 * 3600));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(5 == $this->period_id) //本月
			{
				$period_from = date('Y-m-01');
				$period_to = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(6 == $this->period_id) //下月
			{
				$period_from = date('Y-m-d', strtotime('+1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$period_to = date('Y-m-d', strtotime('+2 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date < ?", $period_to);
			}
		}
		
		if($this->date_start_2)
		{
			$data->where("p.confirmed_time >= ?", $this->date_start_2." 00:00:00");
		}
		
		if($this->date_end_2)
		{
			$data->where("p.confirmed_time <= ?", $this->date_end_2." 23:59:59");
		}
		
		if(!$this->date_start_2 && !$this->date_end_2 && $this->period_id_2)
		{
			if(1 == $this->period_id_2) //今天
			{
				$data->where("date(p.confirmed_time) = ?", date("Y-m-d"));
			}elseif(2 == $this->period_id_2) //昨天
			{
				$ytd = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-1,date("Y")));
				$data->where("date(p.confirmed_time) = ?", $ytd);
			}elseif(3 == $this->period_id_2) //本周
			{
				$period_from = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(4 == $this->period_id_2) //上周
			{
				$period_from = date('Y-m-d', (time() - (6 + (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$period_to = date('Y-m-d', (time() - (date('w') == 0 ? 7 : date('w')) * 24 * 3600));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(5 == $this->period_id_2) //本月
			{
				$period_from = date('Y-m-01');
				$period_to = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(6 == $this->period_id_2) //上月
			{
				$period_from = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$period_to = date("Y-m-01");
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) < ?", $period_to);
			}elseif(7 == $this->period_id_2) //今年
			{
				$period_from = date("Y-01-01");
				$data->where("date(p.confirmed_time) >= ?", $period_from);
			}
		}
		
		if($this->status_type)
		{
			if(1 == $this->status_type)
			{
				$data->where("p.status = ?", 0);
			}elseif(2 == $this->status_type)
			{
				$data->where("p.status = ?", 1);
			}elseif(3 == $this->status_type)
			{
				$data->where("p.status = ?", 0);
				$data->where("p.plan_date < ?", date("Y-m-d"));
			}
		}
		
		if($this->sales_emp_id)
		{
			$data->where("c.sales_emp_id = ?", $this->sales_emp_id);
		}elseif("LEVEL-2" == $_SESSION['identity']['level']) //销售主管只可以看属于自己的员工合同
		{
			$mod_user = new Db_Tables2_User();
			$my_staff_ids = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			
			if(!empty($my_staff_ids))
			{
				$data->where("c.sales_emp_id IN (?)", $my_staff_ids);
			}
		}
		
		if($this->product_id)
		{
			$data->where("s.product_id = ?", $this->product_id);
		}
		
		if($this->keyword)
		{
			$data->where("c.contract_sid like '%".$this->keyword."%' or c.client_name like '%".$this->keyword."%' or c.signer_company_name like '%".$this->keyword."%'");
		}
		
		$ct = $dealers_db->fetchRow($data);
		
		//dump all in condition
		$data = $dealers_db->select();
		$data->from("b_logs_contract_pay_plan as p", array("plan_id", "plan_date", "plan_value", "tips", "invoice", "status", "confirmed_time"));
		$data->joinLeft("b_contract as c", "c.contract_id=p.contract_id", array("contract_id", "contract_sid", "client_name", "signer_company_name", "sales_emp_name"));
		$data->joinLeft("b_contract_sku as s", "s.contract_id=c.contract_id", array("product_name"));
		
		if($this->date_start)
		{
			$data->where("p.plan_date >= ?", $this->date_start);
		}
		
		if($this->date_end)
		{
			$data->where("p.plan_date <= ?", $this->date_end);
		}
		
		if(!$this->date_start && !$this->date_end && $this->period_id)
		{
			if(1 == $this->period_id) //今天
			{
				$data->where("p.plan_date = ?", date("Y-m-d"));
			}elseif(2 == $this->period_id) //明天
			{
				$tmr = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+1,date("Y")));
				$data->where("p.plan_date = ?", $tmr);
			}elseif(3 == $this->period_id) //本周
			{
				$period_from = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(4 == $this->period_id) //下周
			{
				$period_from = date('Y-m-d', (time() + (7 - ((date('w') == 0 ? 7 : date('w')) - 1)) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 + (7 - (date('w') == 0 ? 7 : date('w')))) * 24 * 3600));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(5 == $this->period_id) //本月
			{
				$period_from = date('Y-m-01');
				$period_to = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date <= ?", $period_to);
			}elseif(6 == $this->period_id) //下月
			{
				$period_from = date('Y-m-d', strtotime('+1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$period_to = date('Y-m-d', strtotime('+2 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$data->where("p.plan_date >= ?", $period_from);
				$data->where("p.plan_date < ?", $period_to);
			}
		}
		
		if($this->date_start_2)
		{
			$data->where("p.confirmed_time >= ?", $this->date_start_2." 00:00:00");
		}
		
		if($this->date_end_2)
		{
			$data->where("p.confirmed_time <= ?", $this->date_end_2." 23:59:59");
		}
		
		if(!$this->date_start_2 && !$this->date_end_2 && $this->period_id_2)
		{
			if(1 == $this->period_id_2) //今天
			{
				$data->where("date(p.confirmed_time) = ?", date("Y-m-d"));
			}elseif(2 == $this->period_id_2) //昨天
			{
				$ytd = date("Y-m-d", mktime(0,0,0,date("m"),date("d")-1,date("Y")));
				$data->where("date(p.confirmed_time) = ?", $ytd);
			}elseif(3 == $this->period_id_2) //本周
			{
				$period_from = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
				$period_to = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(4 == $this->period_id_2) //上周
			{
				$period_from = date('Y-m-d', (time() - (6 + (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
				$period_to = date('Y-m-d', (time() - (date('w') == 0 ? 7 : date('w')) * 24 * 3600));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(5 == $this->period_id_2) //本月
			{
				$period_from = date('Y-m-01');
				$period_to = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) <= ?", $period_to);
			}elseif(6 == $this->period_id_2) //上月
			{
				$period_from = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
				$period_to = date("Y-m-01");
				$data->where("date(p.confirmed_time) >= ?", $period_from);
				$data->where("date(p.confirmed_time) < ?", $period_to);
			}elseif(7 == $this->period_id_2) //今年
			{
				$period_from = date("Y-01-01");
				$data->where("date(p.confirmed_time) >= ?", $period_from);
			}
		}
		
		if($this->status_type)
		{
			if(1 == $this->status_type)
			{
				$data->where("p.status = ?", 0);
			}elseif(2 == $this->status_type)
			{
				$data->where("p.status = ?", 1);
			}elseif(3 == $this->status_type)
			{
				$data->where("p.status = ?", 0);
				$data->where("p.plan_date < ?", date("Y-m-d"));
			}
		}
		
		if($this->sales_emp_id)
		{
			$data->where("c.sales_emp_id = ?", $this->sales_emp_id);
		}elseif("LEVEL-2" == $_SESSION['identity']['level']) //销售主管只可以看属于自己的员工合同
		{
			$mod_user = new Db_Tables2_User();
			$my_staff_ids = $mod_user->GetMyStaffIds($_SESSION['identity']['userid']);
			
			if(!empty($my_staff_ids))
			{
				$data->where("c.sales_emp_id IN (?)", $my_staff_ids);
			}
		}
		
		if($this->product_id)
		{
			$data->where("s.product_id = ?", $this->product_id);
		}
		
		if($this->keyword)
		{
			$data->where("c.contract_sid like '%".$this->keyword."%' or c.client_name like '%".$this->keyword."%' or c.signer_company_name like '%".$this->keyword."%'");
		}

		if($this->page_id && !$this->skip_pagination)
		{
			$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
		}
		
		if(2 == $this->status_type)
		{
			$orderby = array("p.confirmed_time DESC", "p.plan_date DESC");
		}else{
			$orderby = "p.plan_date DESC";
		}
		
		$data->order($orderby);
		
		$origine = $dealers_db->fetchAll($data);
		
		$current_page_info = array();
		
		if(!empty($origine))
		{	
			$ids = array();
			
			$current_page_info = array(
				"plan_date" => array(),
				"plan_value" => array(),
				"tips" => array(),
				"invoice" => array(),
				"status" => array(),
				"confirmed_time" => array(),
				"contract_id" => array(),
				"contract_sid" => array(),
				"client_name" => array(),
				"signer_company_name" => array(),
				"sales_emp_name" => array(),
				"product_name" => array()
			);
			
			foreach($origine as $row)
			{
				$current_page_info['plan_date'][$row['plan_id']] = $row['plan_date'];
				$current_page_info['plan_value'][$row['plan_id']] = $row['plan_value'];
				$current_page_info['tips'][$row['plan_id']] = $row['tips'];
				$current_page_info['invoice'][$row['plan_id']] = $row['invoice'];
				$current_page_info['status'][$row['plan_id']] = $row['status'];
				$current_page_info['confirmed_time'][$row['plan_id']] = $row['confirmed_time'];
				$current_page_info['contract_id'][$row['plan_id']] = $row['contract_id'];
				$current_page_info['contract_sid'][$row['plan_id']] = $row['contract_sid'];
				$current_page_info['client_name'][$row['plan_id']] = $row['client_name'];
				$current_page_info['signer_company_name'][$row['plan_id']] = $row['signer_company_name'];
				$current_page_info['sales_emp_name'][$row['plan_id']] = $row['sales_emp_name'];
				$current_page_info['product_name'][$row['plan_id']] = $row['product_name'];
			}
		}
		
		$result = array(
			'ct' => $ct['ct'],
			'sm' => $ct['sm'],
			'total_pages' => ceil($ct['ct'] / $this->page_size),
			'current_page_info' => $current_page_info
		);
//		$mod_ext = new Sys_Extensions();
//		$mod_ext->FormatArray($result);
//		die;
		return $result;
	}
	
}
